package com.ruoyi.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Job;
import com.ruoyi.system.domain.Seeker;
import com.ruoyi.system.service.IJobService;
import com.ruoyi.system.service.ISeekerService;

import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JobSeekerRelation;
import com.ruoyi.system.service.IJobSeekerRelationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;



import org.springframework.web.bind.annotation.RequestParam;


/**
 * 简历投递状态管理Controller
 *
 * @author zyq
 * @date 2024-05-26
 */
@RestController
@RequestMapping("/system/job_seeker_relation")
public class JobSeekerRelationController extends BaseController
{
    @Autowired
    private IJobSeekerRelationService jobSeekerRelationService;
    @Autowired
    private ISeekerService SeekerService;
    @Autowired
    private IJobService jobService;


    /**
     * 查询简历投递状态管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:job_seeker_relation:list')")
    @GetMapping("/list")
    public TableDataInfo list(JobSeekerRelation jobSeekerRelation)
    {
        startPage();
        List<JobSeekerRelation> list = jobSeekerRelationService.selectJobSeekerRelationList(jobSeekerRelation);
        return getDataTable(list);
    }

    @GetMapping("/listByhrid")
    public TableDataInfo listBy(String hrId)
    {
        startPage();
        JobSeekerRelation job = new JobSeekerRelation();
        Long hrIdLong;
        hrIdLong = Long.parseLong(hrId);
        job.setHrId(hrIdLong);

        List<JobSeekerRelation> list = jobSeekerRelationService.selectJobSeekerRelationList(job);

        // 创建一个列表来保存求职者
        List<Seeker> seekers = new ArrayList<>();
        for (JobSeekerRelation relation : list) {
            // 根据seekerId获取每个求职者
            Seeker seeker = SeekerService.selectSeekerBySeekerId(relation.getSeekerId());
            if (seeker != null) {
                seekers.add(seeker);
            }
        }
        return getDataTable(seekers);
        //return getDataTable(list);
    }

    @GetMapping("/listByhridandnotyet")
    public TableDataInfo listByhridandnotyet(String hrId,Long seekerPosition,
    String seekerInfo,String seekerEducation,
    String seekerAdvantage)
    {
        startPage();
        JobSeekerRelation job = new JobSeekerRelation();
        Long hrIdLong;
        hrIdLong = Long.parseLong(hrId);
        job.setHrId(hrIdLong);

        String cvres="0";
        job.setCvResult(cvres);//需要的内容
        job.setJobId(seekerPosition);

        List<JobSeekerRelation> list = jobSeekerRelationService.selectJobSeekerRelationList(job);

        // 创建一个列表来保存求职者
        List<Seeker> seekers = new ArrayList<>();
        // 创建一个列表来保存求职者和关系信息
        List<Map<String, Object>> seekersWithRelations = new ArrayList<>();
        for (JobSeekerRelation relation : list) {
            // 根据seekerId获取每个求职者
            Seeker seeker = SeekerService.selectSeekerBySeekerId(relation.getSeekerId());
            Job jobq=jobService.selectJobByJobId(relation.getJobId());
            if (seeker != null && 
            (seeker.getSeekerName().contains(seekerInfo)&&
            seeker.getSeekerSchname().contains(seekerEducation)&&
            seeker.getSeekerAdv().contains(seekerAdvantage)
            )) {
                seekers.add(seeker);
                Map<String, Object> map = new HashMap<>();
                map.put("seekerInfo", seeker);
                map.put("relationInfo", relation);
                map.put("jobinfo",jobq);
                seekersWithRelations.add(map);
            }
        }
        return getDataTable(seekersWithRelations);
        //return getDataTable(list);
    }

    @GetMapping("/listByhrid_already")
    public TableDataInfo listByhrid_already(String hrId,Long seekerPosition,Long status)
    {
        startPage();
        JobSeekerRelation job = new JobSeekerRelation();
        Long hrIdLong;
        hrIdLong = Long.parseLong(hrId);
        job.setHrId(hrIdLong);
        String cvres;
        
        // 创建一个列表来保存求职者
        List<Seeker> seekers = new ArrayList<>();
        // 创建一个列表来保存求职者和关系信息
        List<Map<String, Object>> seekersWithRelations = new ArrayList<>();


        if(status!=2){//通过
        cvres="1";
        job.setCvResult(cvres);
        job.setJobId(seekerPosition);

        List<JobSeekerRelation> list = jobSeekerRelationService.selectJobSeekerRelationList(job);

        for (JobSeekerRelation relation : list) {
            // 根据seekerId获取每个求职者
            Seeker seeker = SeekerService.selectSeekerBySeekerId(relation.getSeekerId());
            Job jobq=jobService.selectJobByJobId(relation.getJobId());

            if (seeker != null) {
                seekers.add(seeker);
                Map<String, Object> map = new HashMap<>();
                map.put("seekerInfo", seeker);
                map.put("relationInfo", relation);
                map.put("jobinfo",jobq);
                seekersWithRelations.add(map);
            }
        }
    }

    if(status!=1){//拒绝
        cvres="2";
        job.setCvResult(cvres);
        job.setJobId(seekerPosition);

        List<JobSeekerRelation> list2 = jobSeekerRelationService.selectJobSeekerRelationList(job);

        for (JobSeekerRelation relation : list2) {
            // 根据seekerId获取每个求职者
            Seeker seeker = SeekerService.selectSeekerBySeekerId(relation.getSeekerId());
            Job jobq=jobService.selectJobByJobId(relation.getJobId());

            if (seeker != null) {
                seekers.add(seeker);
                Map<String, Object> map = new HashMap<>();
                map.put("seekerInfo", seeker);
                map.put("relationInfo", relation);
                map.put("jobinfo",jobq);
                seekersWithRelations.add(map);
            }
        }
    }

        return getDataTable(seekersWithRelations);
    }

    @RequestMapping("/approve")
    public AjaxResult approve(@RequestParam Long relationId)//@RequestParam String hrid
    {
        if (relationId == null) {
            return AjaxResult.error("seekerId cannot be null");
        }
        startPage();

        //JobSeekerRelation job = new JobSeekerRelation();
//        Long hrIdLong;
//        hrIdLong = Long.parseLong(hrId);

        //job.setRelationId(relationId);

        JobSeekerRelation relation = jobSeekerRelationService.selectJobSeekerRelationByRelationId(relationId);

        // 创建一个列表来保存求职者
        // List<Seeker> seekers = new ArrayList<>();
            String cvres="1";
            relation.setCvResult(cvres);


        return toAjax(jobSeekerRelationService.updateJobSeekerRelation(relation));
    }

    @RequestMapping("/reject")
    public AjaxResult reject(@RequestParam Long relationId)
    {
        if (relationId == null) {
            return AjaxResult.error("seekerId cannot be null");
        }
        startPage();
        JobSeekerRelation relation = jobSeekerRelationService.selectJobSeekerRelationByRelationId(relationId);
        String cvres="2";
        relation.setCvResult(cvres);
        return toAjax(jobSeekerRelationService.updateJobSeekerRelation(relation));
    }




    @GetMapping("/count")
    public int count(Long jobId,String result)
    {
        JobSeekerRelation jobSeekerRelation = new JobSeekerRelation();
        jobSeekerRelation.setJobId(jobId);
        jobSeekerRelation.setCvResult(result);
        int num = jobSeekerRelationService.countJobSeekerRelationByConditions(jobSeekerRelation);
        num = (num == 0) ?0:num;
        return num;
    }

    /**
     * 导出简历投递状态管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:job_seeker_relation:export')")

    @PostMapping("/export")
    public void export(HttpServletResponse response, JobSeekerRelation jobSeekerRelation)
    {
        List<JobSeekerRelation> list = jobSeekerRelationService.selectJobSeekerRelationList(jobSeekerRelation);
        ExcelUtil<JobSeekerRelation> util = new ExcelUtil<JobSeekerRelation>(JobSeekerRelation.class);
        util.exportExcel(response, list, "简历投递状态管理数据");
    }

    /**
     * 获取简历投递状态管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:job_seeker_relation:query')")
    @GetMapping(value = "/{relationId}")
    public AjaxResult getInfo(@PathVariable("relationId") Long relationId)
    {
        return success(jobSeekerRelationService.selectJobSeekerRelationByRelationId(relationId));
    }

    /**
     * 新增简历投递状态管理
     */

    @PostMapping
    public AjaxResult add(@RequestBody JobSeekerRelation jobSeekerRelation)
    {
        return toAjax(jobSeekerRelationService.insertJobSeekerRelation(jobSeekerRelation));
    }

    /**
     * 修改简历投递状态管理
     */
    @PreAuthorize("@ss.hasPermi('system:job_seeker_relation:edit')")

    @PutMapping
    public AjaxResult edit(@RequestBody JobSeekerRelation jobSeekerRelation)
    {
        return toAjax(jobSeekerRelationService.updateJobSeekerRelation(jobSeekerRelation));
    }

    /**
     * 删除简历投递状态管理
     */
    @PreAuthorize("@ss.hasPermi('system:job_seeker_relation:remove')")

    @DeleteMapping("/{relationIds}")
    public AjaxResult remove(@PathVariable Long[] relationIds)
    {
        return toAjax(jobSeekerRelationService.deleteJobSeekerRelationByRelationIds(relationIds));
    }
}
